package org.softmed.action.resolver;

import java.util.List;

import org.softmed.action.Action;
import org.softmed.action.ActionStep;
import org.softmed.action.description.ActionDescription;
import org.softmed.action.description.ActionStepDescription;
import org.softmed.action.description.StateManager;
import org.softmed.action.states.State;

import com.google.inject.Inject;

public class DefaultActionResolver implements ActionResolver {

	@Inject
	ActionStepResolverManager actionStepResolverManager;

	public Action instantiate(ActionDescription description) throws Throwable {
		Action action = new Action();
		instantiate(action.getTest(), description.getTest());
		instantiate(action.getSetup(), description.getSetup());
		instantiate(action.getAsserting(), description.getAssertion());
		instantiate(action.getExecuting(), description.getExecute());
		instantiate(action.getExecutionFailure(), description
				.getExecutionFailure());
		instantiate(action.getExecutionSuccess(), description
				.getExecutionSuccess());
		instantiate(action.getDisposing(), description.getDispose());
		instantiate(action.getPreparationFailure(), description
				.getPreparationFailure());
		return action;
	}

	private void instantiate(State test, StateManager test2) throws Throwable {
		if (test2 == null)
			return;

		List<ActionStepDescription> steps = test2.getActionSteps();
		ActionStep step = null;
		for (ActionStepDescription description : steps) {
			step = actionStepResolverManager.resolve(description);
			test.addStep(step);
		}

	}

	public ActionStepResolverManager getActionStepResolverManager() {
		return actionStepResolverManager;
	}

	public void setActionStepResolverManager(
			ActionStepResolverManager actionStepResolverManager) {
		this.actionStepResolverManager = actionStepResolverManager;
	}

}
